package com.suning.sawp.service.impl.customer;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.customer.CustRemarkInfoRtn;
import com.suning.sawp.intf.customer.CustRemarkService;
import com.suning.sawp.po.customer.CustRemarkInfo;
import com.suning.sawp.service.util.DJStringUtils;

/**
 * 查询客户信息具体实现
 * @author 14062606
 *
 */
@Service
public class CustRemarkServiceImpl implements CustRemarkService{

	@Resource
    MongoTemplate mongoTemplate;
	
	@Override
	public ReturnMsg<CustRemarkInfoRtn> queryCustInfoByCustNum(String custNum,String staffId){
		ReturnMsg<CustRemarkInfoRtn> ret = new ReturnMsg<CustRemarkInfoRtn>();
		Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("custNum").is(custNum);
        criteria.and("staffId").is(staffId);
        query.addCriteria(criteria);
        CustRemarkInfo custInfo = mongoTemplate.findOne(query, CustRemarkInfo.class);
        CustRemarkInfoRtn rtn = new CustRemarkInfoRtn();
        if (null != custInfo){
        	rtn.setCommodityName(custInfo.getCommodityName() == null ? "" : custInfo.getCommodityName());
        	rtn.setHopeBuyDate(custInfo.getHopeBuyDate());
        	rtn.setHopePrice(custInfo.getHopePrice() == null ? "" : custInfo.getHopePrice());
        	String url = DJStringUtils.getMemberProtraitUrl(custNum);
        	if (StringUtils.isNotBlank(url)){
        		rtn.setMemberPortraitUrl(url);
        	}else {
        		rtn.setMemberPortraitUrl("");
        	}
        	rtn.setRemark(custInfo.getRemark() == null ? "" : custInfo.getRemark());
        	rtn.setTag(custInfo.getTag() == null ? "" : custInfo.getTag());
        } else {
        	rtn.setCommodityName("");
        	rtn.setHopeBuyDate(null);
        	rtn.setHopePrice("");
        	rtn.setMemberPortraitUrl("");
        	rtn.setRemark("");
        	rtn.setTag("");
        }
        ret.setData(rtn);
		return ret;
	}

	@Override
	public void saveCustRemarkInfo(String staffId, String custNum, String commodityName,
			String hopePrice, Date hopeBuyDate, String remark, String tag) {
		Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("staffId").is(staffId);
        criteria.and("custNum").is(custNum);
        query.addCriteria(criteria);
		
        Update update = new Update();
        update.set("staffId", staffId);
        update.set("custNum", custNum);
        update.set("commodityName", commodityName);
        update.set("hopePrice", hopePrice);
        update.set("hopeBuyDate", hopeBuyDate);
        update.set("remark", remark);
        update.set("tag", tag);
		mongoTemplate.upsert(query, update, CustRemarkInfo.class);
	}

}
